package com.mrd.gtimpframe.service.impl;

import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.business.service.FundAccountBiz;
import com.mrd.gtimpframe.service.AccountService;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;

/**
 * Created by dusy on 2017/7/24.
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private FundAccountBiz fundAccountBiz;
    /**
     * 查询当前用户当前时间点的公司余额
     */
    @Override
    public Result getCurrentBalance() {
        User user = LoginUtil.getLoginUser();
        String balance = "";
        try {
            balance = fundAccountBiz.findBalanceByCompanyId(user.getCompany().getId());
            if (!StringUtils.isEmpty(balance)){
                double remaining = Double.parseDouble(balance);
                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                balance = decimalFormat.format(remaining);
            }else {
                balance = "0.00";
            }
        } catch (QueryException e) {
            e.printStackTrace();
            return Result.newFailInstance("暂时无法读取余额");
        }
        return Result.newSuccessInstance(balance);
    }
}
